package com.uenpay.im.ui.old.activity.contacts;

import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.utils.Utils;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.im.entities.QYXUserEntity;
import com.uenpay.util.app.base.BaseActivity;

import java.util.ArrayList;

/**
 * 搜索好友
 *
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */

public class SearchFriendsActivity extends BaseActivity {

    private EditText search_phone_edit;
    private View loading;
    private LinearLayout no_data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
        Utils.showSoftKeyboard(search_phone_edit);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_search_friends_layout;
    }

    @Override
    public void initView(View view) {
        initView();
        initListener();
    }

    protected void initView() {
        ((TextView) findViewById(R.id.title_textview)).setText(getResources()
                .getString(R.string.add_friends));
        ((TextView) findViewById(R.id.title_right_tv)).setText(getResources()
                .getString(R.string.search));
        findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
        search_phone_edit = (EditText) findViewById(R.id.search_phone_edit);
        loading = findViewById(R.id.loading);
        no_data = (LinearLayout) findViewById(R.id.no_data);
    }

    protected void initListener() {
        findViewById(R.id.back_layout).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        Utils.hideSoftKeyboard(search_phone_edit);
                        new Handler().postAtTime(new Runnable() {

                            @Override
                            public void run() {
                                SearchFriendsActivity.this.finish();
                            }
                        }, 200);
                    }
                });
        findViewById(R.id.title_right_layout).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        String phoneNum = search_phone_edit.getText()
                                .toString();
                        if (!TextUtils.isEmpty(phoneNum)) {
                            Utils.hideSoftKeyboard(search_phone_edit);
                            searchFriendsByPhoneNum(phoneNum);
                        } else {
                            IMApplication.showToast(getResources().getString(
                                    R.string.input_phone_number));
                        }
                    }
                });
    }

    /**
     * 搜索用户
     *
     * @param phoneNum
     */
    private void searchFriendsByPhoneNum(String phoneNum) {
        loading.setVisibility(View.VISIBLE);
        no_data.setVisibility(View.GONE);

        ContactsHandle.searchAccount(phoneNum, new ContactsHandle.ISearchAccountListener() {

            @Override
            public void onSearchAccountResult(int status, String msg,
                                              ArrayList<QYXUserEntity> arrayList) {
                myHandler.sendEmptyMessage(0);
                if (status == HttpInvokeResultCode.RESULT_OK) {
                    if (arrayList != null && arrayList == null
                            || arrayList.size() < 1) {
                        myHandler.sendEmptyMessage(1);
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("result", arrayList);
                        ActivityHandle.startActivity(
                                SearchFriendsActivity.this,
                                FriendsListActivity.class, bundle, -1);
                        Utils.hideSoftKeyboard(search_phone_edit);
                    }
                } else if (!TextUtils.isEmpty(msg)) {
                    IMApplication.showToast(msg);
                }
            }
        });

    }

    private Handler myHandler = new Handler(new Callback() {

        @Override
        public boolean handleMessage(Message msg) {

            if (msg.what == 0) {
                if (loading != null) {
                    loading.setVisibility(View.GONE);
                }
            } else if (msg.what == 1) {
                if (no_data != null) {
                    no_data.setVisibility(View.VISIBLE);
                }
            }

            return false;
        }
    });

}
